package com.ruoyi.task.service.impl;

import com.ruoyi.task.domain.TaskConfig;
import com.ruoyi.task.service.ITaskBatchService;
import com.ruoyi.task.service.ITaskConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;

@Slf4j
@Service
public class TaskBatchServiceImpl implements ITaskBatchService {

    @Autowired
    private ITaskConfigService taskConfigService;

    @Autowired
    private TaskTypeDealService taskTypeDealService;

    @Override
    public void generateTaskBatch() {
        TaskConfig taskConfig = new TaskConfig();
        //status 0-为正常
        taskConfig.setStatus("0");
        taskConfig.setTaskType("2");
        List<TaskConfig> taskConfigList = taskConfigService.selectTaskConfigList(taskConfig);
        if (!CollectionUtils.isEmpty(taskConfigList)) {
            taskConfigList.forEach(config -> {
                taskTypeDealService.setTaskConfig(config);
                taskTypeDealService.dealTaskTypeService(config.getFrequencyType());
            });
        }
    }

    @Override
    public void generateMainTaskBatch(int length) {
        log.info("generateMainTaskBatch begin, length = {}", length);
        List<TaskConfig> taskConfigList = taskConfigService.queryTaskConfigList(length, "1");
        if (!CollectionUtils.isEmpty(taskConfigList)) {
            log.info("generateMainTaskBatch length = {}, size = {}", length, taskConfigList.size());
            int size = taskConfigList.size();
            if (length <= 0 || length > size) {
                length = size;
            }
            for (int i = 0; i < length; i++) {
                taskConfigService.generateMainTask(taskConfigList.get(i));
            }
        }
        log.info("generateMainTaskBatch end");
    }


    @Override
    public void generateBranchHyTaskBatch() {
        log.info("generateHyTaskBatch begin");
        List<TaskConfig> taskConfigList = taskConfigService.queryTaskConfigList(0, "3");
        if (!CollectionUtils.isEmpty(taskConfigList)) {
            log.info("generateHyTaskBatch size = {}", taskConfigList.size());
            for (TaskConfig taskConfig : taskConfigList) {
                taskConfigService.generateBranchHyTask(taskConfig);
            }
        }
        log.info("generateHyTaskBatch end");
    }
}
